import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import createLogger from 'vuex/dist/logger'

var scrollAction = {
	x: 'undefined',
	y: 'undefined'
},
scrollDirection;

//全局引用
Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

const store = new Vuex.Store({
	state,//左右相同可只写一个
	getters:{
		getCount:(state)=>{
			return state.count=state.count<10?"0"+state.count:state.count
		},
		getCurTid:(state)=>{
			let partitionList=window.__INITIAL_STATE__.reduxAsyncConnect.partitionList
			const hasCon=partitionList[state.curTid]
			if(!hasCon){
				const keyArr=Object.keys(partitionList)//存入键名
				for(let i=0,len=keyArr.length;i<len;i++){
					const tmpArr=partitionList[keyArr[i]]
					for(let j=0;j<tmpArr.length;j++){
						if(tmpArr[j].tid==state.curTid){
							return state.curTid=keyArr[i]
						}
					}
				}
			}else{
				return state.curTid
			}
		},
		getScrAct:(state)=>{
			return state.scr_act
		},
		getIsLogin:(state)=>{
			return state.isLogin
		}
	},
	mutations:{
		increment (state){
			state.count++;
		},
		setCurTid(state,payload){
			state.curTid=payload.curTid
		},
		scrollEvent(state){
			var doc=window.pageYOffset;
				store.commit("scrollFunc")
				if(scrollDirection == 'down') {
					//页面向下滚动要做的事情
					if(doc>=30){
						state.scr_act=true
					}
//					console.log(state.scr_act)
				} else if(scrollDirection == 'up') {
					//页面向上滚动要做的事情
						state.scr_act=false
//					console.log(state.scr_act)
				}
		},
		scrollFunc(){
			if(typeof scrollAction.x == 'undefined') {
				scrollAction.x = window.pageXOffset;
				scrollAction.y = window.pageYOffset;
			}
			var diffX = scrollAction.x - window.pageXOffset;
			var diffY = scrollAction.y - window.pageYOffset;
			if(diffX < 0) {
				// Scroll right
				scrollDirection = 'right';
			} else if(diffX > 0) {
				// Scroll left
				scrollDirection = 'left';
			} else if(diffY < 0) {
				// Scroll down
				scrollDirection = 'down';
			} else if(diffY > 0) {
				// Scroll up
				scrollDirection = 'up';
			} else {
				// First scroll event
			}
			scrollAction.x = window.pageXOffset;
			scrollAction.y = window.pageYOffset;
		},
		setIsLogin(a){
			state.isLogin=a.isLogin
		}
	},
//	plugins:debug ?[createLogger()] : []
})

export default store
